package com.wlyuan.open.application.listener;

import com.wlyuan.core.domain.event.DomainEventListener;
import com.wlyuan.open.domain.client.event.ApprovedEvent;
import com.wlyuan.open.infrastructure.config.OpenSmsProperties;
import com.wlyuan.sms.client.AliyunSmsClient;
import com.wlyuan.sms.client.AliyunSmsTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ApprovedEventListener implements DomainEventListener<ApprovedEvent> {

    private final OpenSmsProperties openSmsProperties;
    private final AliyunSmsClient aliyunSmsClient;

    @Override
    @Subscribe(sticky = true, threadMode = ThreadMode.ASYNC)
    public void handle(ApprovedEvent event) {
        logger.info("client同意短信通知");
        var client = event.getOpenClient();
        AliyunSmsTemplate template = new AliyunSmsTemplate();
        template.setTemplate(openSmsProperties.getApproveTemplate());
        template.setSign(openSmsProperties.getSignName());
        aliyunSmsClient.send(client.getMobile(), template, null);
    }
}
